home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest1_0.lha / Tests / exer / pingpong.C < prev    next >
C/C++ Source or Header  |  1991-12-11  |  2KB  |  97 lines

  1. #include <stdio.h>
  2. #include <stream.h>
  3. #include "presto.h"
  4.  
  5. static shared_t int switchcount = 10;
  6. static shared_t int verbose = 0;
  7.  
  8. int
  9. Main::init()
  10. {
  11.     nummainthreads = 2;
  12.     for (argc--, argv++; *argv && **argv == '-'; argv++, argc--)
  13.         switch (*(*argv + 1)) {
  14. #ifdef sequent
  15. #ifdef i386
  16.                 case 'a':
  17.                         affinity = 1;
  18.                 break;
  19. #endif /* i386 */
  20. #endif /* sequent */
  21.             case 'q':
  22.                 quantum = atoi(*argv + 2);
  23.                 break;
  24.             case 'n':
  25.                 numprocessors = atoi(*argv + 2);
  26.                 break;
  27.             case 't':
  28.                 nummainthreads = atoi(*argv + 2);
  29.                 if (nummainthreads < 2)
  30.                     nummainthreads = 2;
  31.                 break;
  32.             case 'v':
  33.                 verbose++;
  34.                 break;
  35.             case 's':
  36.                 switchcount = atoi(*argv + 2);
  37.                 break;
  38.             default:
  39.                 cerr << chr(*(*argv + 1)) << " unknown flag.\n";
  40.                     return -1;
  41.         }
  42.     switchcount = switchcount * nummainthreads;
  43.     return 0;
  44. }
  45.  
  46.  
  47.  
  48. static shared_t Monitor m("main_monitor");
  49. static shared_t Condition c(m ,"condition");
  50.  
  51. #define SHOWME        { if (verbose) {cout << thisproc->name() << ":" << \
  52.                        thisthread->tid() << "," << mytid \
  53.                        << ">" << switchcount << "\n"; }}
  54.  
  55. static shared_t int occupied = 0;
  56.  
  57. int
  58. Main::main()
  59. {
  60.  
  61.     register int  mytid = thisthread->tid();
  62.  
  63.     {
  64.     MONITOR ENTRY(m);
  65.     if (!occupied)    {
  66.         if (verbose)
  67.             cout << "Not occupied " << thisthread->name() << "\n";
  68.         occupied++;
  69.         do    {
  70.             c.wait();
  71.             SHOWME;
  72.             c.signal();
  73.         } while (switchcount-- > 0);
  74.         c.broadcast();
  75.     } else    {
  76.         if (verbose)
  77.             cout << "occupied " << thisthread->name() << "\n";
  78.         do     {
  79.             c.signal();
  80.             c.wait();
  81.             SHOWME;
  82.         } while (switchcount-- > 0);
  83.         c.broadcast();
  84.     }
  85.     if (verbose)
  86.         cout << thisthread->name() << " Finished:" << switchcount << "\n";
  87.     }    // exit monitor here
  88.     return 0;
  89. }
  90.  
  91. int
  92. Main::done()
  93. {
  94.     printf("Success.\n", switchcount);
  95.     return 0;
  96. }
  97.